﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Rendering
{
    using Acoris.Animation;
    using Acoris.Template;
    using Acoris.Foundation;

    /// <summary>
    /// 表示渲染元素的基类
    /// </summary>
    public abstract class VisualElement:IDisposable,IPositionService
    {
        private bool m_cacheResult; // 减少绘图资源的创建
        private RenderFormat m_renderFormat;

        #region Constructor

        /// <summary>
        /// 初始化可视化元素
        /// </summary>
        protected VisualElement() { }

        /// <summary>
        /// 使用指定的父级元素初始化
        /// </summary>
        /// <param name="parent"></param>
        protected VisualElement(VisualElement parent) {
            Parent = parent;
        }

        #endregion

        #region Method

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        protected T Cache<T>(T value)
        {
            m_cacheResult = true;
            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        protected T SetUncacheResult<T>(T value)
        {
            m_cacheResult = false;
            return value;
        }

        /// <summary>
        /// 合成
        /// </summary>
        public virtual void Composition(VisualElement visualElement)
        {
        }

        /// <summary>
        /// 创建绘图相关资源的方法
        /// </summary>
        /// <param name="factory">使用指定的绘图资源的创建资源工厂接口</param>
        internal abstract void CreateResources(IDrawingContext factory);

        /// <summary>
        /// 渲染当前元素
        /// </summary>
        /// <param name="drawingContext">使用指定的绘图上下文</param>
        internal abstract void Render(IDrawingContext drawingContext);

        /// <summary>
        /// 释放占用的资源
        /// </summary>
        public abstract void Dispose();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public Position GetPosition(VisualElement element)
        {
            return default(Position);
        }

        #endregion  

        #region Public Property

        /// <summary>
        /// 获取或设置当前可视化元素的父级元素
        /// </summary>
        public VisualElement Parent { get; protected set; }

        /// <summary>
        /// 获取当前元素的缓冲结果
        /// </summary>
        public bool CacheResult {
            get {
                return m_cacheResult;
            }
        }

        /// <summary>
        /// 获取或设置当前元素的实际可视化高度
        /// </summary>
        public float ActualHeight { get; set; }

        /// <summary>
        /// 获取或设置当前元素的实际可视化宽度
        /// </summary>
        public float ActualWidth { get; set; }

        /// <summary>
        /// 获取或设置可视化元素的宽度
        /// </summary>
        public float Width { get; set; }

        /// <summary>
        /// 获取或设置可视化元素的高度
        /// </summary>
        public float Height { get; set; }

        /// <summary>
        /// 获取或设置当前元素的渲染格式
        /// </summary>
        public RenderFormat Format {
            get {
                return m_renderFormat;
            }
            set {                
                m_renderFormat = SetUncacheResult(value);
            }
        }

        /// <summary>
        /// 获取或设置当前元素的唯一ID值
        /// </summary>
        /// <remarks>该元素的正确值范围在0-2^31，-1代表该值未赋值</remarks>
        public int Id { get; set; } = -1;

        #endregion
    }
}
